package com.gm.wholesale.module.good.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.interfaces.ICallBackListener;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.good.bean.GoodListBean;
import com.gm.wholesale.module.good.model.GoodModel;

public class GoodFunctionView extends LinearLayout {
    private LinearLayout deleteGoodView;
    private ImageView image;
    private TextView textView;
    private LinearLayout updateGoodStateView;

    private Context mContext;

    public GoodFunctionView(Context context) {
        super(context);
        initView(context);
    }

    public GoodFunctionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_good_function, this);
        deleteGoodView = findViewById(R.id.deleteGoodView);
        image = findViewById(R.id.image);
        textView = findViewById(R.id.textView);
        updateGoodStateView = findViewById(R.id.updateGoodStateView);

    }

    public void showView(GoodListBean goodListBean, ICallBackListener<GoodListBean> iCallBackListener) {
        if (goodListBean == null) return;
        setVisibility(VISIBLE);
        if (goodListBean.isOnShelf()) {
            image.setImageResource(R.drawable.xiajia);
            textView.setText("下架");
        } else {
            image.setImageResource(R.drawable.shangjia);
            textView.setText("上架");
        }
        deleteGoodView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodModel.deleteGood((IBaseView) mContext, goodListBean.getId(), goodListBean.getVersion(), new BaseHttpObserver<BaseResp>() {
                    @Override
                    protected void handleResultOkResp(BaseResp resp) {
                        ToastUtil.showToast("删除成功");
                        goodListBean.setHandleType(1);
                        iCallBackListener.onResult(goodListBean, null);
                    }
                });
            }
        });
        updateGoodStateView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodModel.updateGoodState((IBaseView) mContext, goodListBean.getId(), !goodListBean.isOnShelf(), new BaseHttpObserver<BaseResp>() {
                    @Override
                    protected void handleResultOkResp(BaseResp resp) {
                        ToastUtil.showToast("操作成功");
                        goodListBean.setOnShelf(!goodListBean.isOnShelf());
                        iCallBackListener.onResult(goodListBean, null);
                    }
                });
            }
        });
    }

    public void hidView() {
        setVisibility(GONE);
    }
}
